from common.sqlalchemy_package import *
from flask import g
from datetime import datetime as dt #两者不一样
import datetime

class Vip(BaseModel):
    __tablename__ = 'vip'
    vip_id = mapped_column(Integer, primary_key=True)  # 用户id
    car_number = mapped_column(String)  # 用户车牌
    vip_month_card = mapped_column(String) #月卡
    vip_year_card= mapped_column(String) # 年卡
    vip_open_month_card_time=mapped_column(DateTime) #月卡开通时间
    vip_open_year_card_time=mapped_column(DateTime) #年卡开通时间



    #查询所有vip用户信息
    @staticmethod
    def query_vip_all():
        return g.db_session.query(Vip).all()
    #分页获取开卡信息
    @staticmethod
    def query_vip_page_all(page):
        per_page = 8  # 每页返回的数据量
        start_index = (int(page) - 1) * per_page  # 计算每页数据的起始索引
        query = g.db_session.query(Vip)
        # 查询满足条件的记录总数
        total_count = query.count()
        # 计算总页数
        total_pages = (total_count + per_page - 1) // per_page
        # 分页查询并逆向排序
        results = query.order_by(Vip.vip_id.desc()).limit(per_page).offset(start_index).all()
        return results, total_pages

    # 根据车牌号模糊查询vip信息
    @staticmethod
    def vip_car_number_search(car_number):
        return g.db_session.query(Vip).filter(Vip.car_number.like('%' + car_number + '%')).order_by(
            Vip.vip_id.desc()).all()

    # 根据vip_id查询信息
    @staticmethod
    def query_vip_id_first(vip_id):
        return g.db_session.query(Vip).filter(Vip.vip_id == int(vip_id)).first()

    #查询开通月卡的全部信息
    @staticmethod
    def query_open_vip_month_all():
        return g.db_session.query(Vip).filter(Vip.vip_month_card=='True').all()

    # 查询开通年卡的全部信息
    @staticmethod
    def query_open_vip_year_all():
        return g.db_session.query(Vip).filter(Vip.vip_year_card == 'True').all()

    # 根据车牌号查询vip
    @staticmethod
    def query_car_number_vip(car_number):
        return g.db_session.query(Vip).filter(Vip.car_number == car_number).first()

    # 根据车牌号查询是否开通vip
    @staticmethod
    def query_car_number_is_vip(car_number):
        query=g.db_session.query(Vip).filter(Vip.car_number == car_number).first()
        if query is not None:
            if query.vip_month_card=='True' or query.vip_year_card=='True':
                return 0
            else:
                return -1
        else:
            return -1

    #保存VIP用户信息
    @staticmethod
    def add_vip_info(car_number,vip_month_card,vip_open_month_card_time,vip_year_card,vip_open_year_card_time):
        try:
            vip=Vip(car_number=car_number,vip_month_card=vip_month_card,vip_year_card=vip_year_card,
                    vip_open_month_card_time=vip_open_month_card_time,
                    vip_open_year_card_time=vip_open_year_card_time)
            g.db_session.add(vip)
            g.db_session.commit()
            return 0
        except Exception as e:
            print(e)
            return -1

    #判断是否开哪种类型的卡
    @staticmethod
    def modify_is_open_card(car_number,vip_month_card,vip_year_card):
        query=Vip.query_car_number_vip(car_number)
        # if query.vip_month_card == 'True':
        #     return 1 #已经开月卡不能再开
        # elif query.vip_year_card=='True':
        #     return 2  # 已经开年卡不能再开
        # elif query.vip_month_card=='True' or query.vip_year_card=='True':
        #     return 3 #已经开卡不能再开
        # else:
        #此时没开通任何卡,判断当月卡为不为空时，开通月卡
        if vip_month_card:
            query.vip_month_card='True'
            return 4
        elif vip_year_card:
            query.vip_year_card = 'True'
            return 5
        else:
            return 6

    #修改月卡会员状态
    @staticmethod
    def modify_vip_month_state(vip_id):
        try:
            query=g.db_session.query(Vip).filter(Vip.vip_id==int(vip_id)).first()
            query.vip_month_card ='False'
            query.vip_open_month_card_time=None
            g.db_session.commit()
            return 0
        except Exception as e:
            return -1

    # 修改年卡会员状态
    @staticmethod
    def modify_vip_year_state(vip_id):
        try:
            query = g.db_session.query(Vip).filter(Vip.vip_id == int(vip_id)).first()
            query.vip_year_card = 'False'
            query.vip_open_year_card_time = None
            g.db_session.commit()
            return 0
        except Exception as e:
            return -1
    #修改月卡会员状态，开启和关闭会员
    @staticmethod
    def modify_is_month_card(vip_id):
        try:
            query = g.db_session.query(Vip).filter(Vip.vip_id == int(vip_id)).first()
            if query:
                if query.vip_year_card=='False': #如果年卡已开不可再更改
                    if query.vip_month_card=='True':
                        query.vip_month_card='False'
                        query.vip_open_month_card_time=None
                        g.db_session.commit()
                        return 0
                    else:
                        query.vip_month_card = 'True'
                        query.vip_open_month_card_time = dt.now()
                        g.db_session.commit()
                        return 1
                else:
                    return 2
            else:
                return -1
        except Exception as e:
            print(e)
            return -1

    # 修改年卡会员状态，开启和关闭会员
    @staticmethod
    def modify_is_year_card(vip_id):
        try:
            query = g.db_session.query(Vip).filter(Vip.vip_id == int(vip_id)).first()
            if query:
                if query.vip_month_card=='False': #如果月卡已开不可再更改
                    if query.vip_year_card == 'True':
                        query.vip_year_card = 'False'
                        query.vip_open_year_card_time = None
                        g.db_session.commit()
                        return 0
                    else:
                        query.vip_year_card = 'True'
                        query.vip_open_year_card_time = dt.now()
                        g.db_session.commit()
                        return 1
                else:
                    return 2
            else:
                return -1
        except:
            return -1


  #修改月卡会员开通状态
    @staticmethod
    def modify_open_month_state(car_number,time):
        try:
            query=g.db_session.query(Vip).filter(Vip.car_number==car_number).first()
            query.vip_month_card ='True'
            query.vip_open_month_card_time=time
            g.db_session.commit()
            return 0
        except Exception as e:
            return -1

    # 修改年卡会员开通状态
    @staticmethod
    def modify_open_year_state(car_number, time):
        try:
            query = g.db_session.query(Vip).filter(Vip.car_number == car_number).first()
            query.vip_year_card = 'True'
            query.vip_open_year_card_time = time
            g.db_session.commit()
            return 0
        except Exception as e:
            return -1

    #根据车牌号查询该车牌是否开卡,以及开卡的类型
    @staticmethod
    def query_car_number_card(car_number):
        query = g.db_session.query(Vip).filter(Vip.car_number == car_number).first()
        if query:
            if query.vip_month_card == 'True':
                vipopentime = query.vip_open_month_card_time
                delta = datetime.timedelta(days=30)
                past = vipopentime + delta  # 月卡会员过期时间
                return ['月卡', str(vipopentime), str(past)]
            elif query.vip_year_card == 'True':
                vipopentime = query.vip_open_year_card_time
                delta = datetime.timedelta(days=365)
                past = vipopentime + delta  # 年卡会员过期时间
                return ['年卡', str(vipopentime), str(past)]
            else:
                return ['未开卡', '未开卡', '未开卡']
        else:
            return ['未开卡', '未开卡', '未开卡']


    #添加会员
    @staticmethod
    def add_car_number_vip(car_number,type):
        query = g.db_session.query(Vip).filter(Vip.car_number == car_number).first()
        if query is None:#代表该车牌未开通vip
            if type=='month':
                vip=Vip(car_number=car_number,vip_month_card='True',vip_year_card='False',
                    vip_open_month_card_time= dt.now(),
                    vip_open_year_card_time=None)
                g.db_session.add(vip)
                g.db_session.commit()
                return 1
            else:
                vip = Vip(car_number=car_number, vip_month_card='False', vip_year_card='True',
                          vip_open_month_card_time=None,
                          vip_open_year_card_time=dt.now())
                g.db_session.add(vip)
                g.db_session.commit()
                return 2
        else:
            return -1 #表示开通过














